<html>
  <head> 
    <link type="text/css" rel="stylesheet" href="/static/main.css" />
    <script src="http://www.google.com/jsapi" type="text/javascript"></script>
    <script type="text/javascript">
    google.load('search', '1');
    
    function searchComplete{{ forloop.counter }}(imageSearch, tag) {
      if (imageSearch.results 
          && imageSearch.results.length > 0) {
        var contentDiv = document.getElementById(tag);
        contentDiv.innerHTML = '';
        var results = imageSearch{{ forloop.counter }}.results;
        var result = results[0];
        var newImg = document.createElement('img');
        newImg.src = result.tbUrl;
        contentDiv.appendChild(newImg);
      }
    }
    
    function doSearch(query, container) {
      imageSearch = new google.search.ImageSearch();
      imageSearch.setResultSetSize(1)
      imageSearch.setRestriction(google.search.Search.SAFESEARCH_STRICT,
               google.search.Search.SAFESEARCH_STRICT);
      imageSearch.setSearchCompleteCallback(this, searchComplete, 
                                            [imageSearch, container]);
      imageSearch.execute(query);
    }

    function OnLoad() {     
      {% for entry in entries %}doSearch('{{ entry.terms|slice:":4"|join:" " }}', 'content{{ forloop.counter }}');
      {% endfor%}
    }
    google.setOnLoadCallback(OnLoad);
    </script>
  </head>
  <body>
    <table width="760px">
      <tr><td>
          <form action="/add" method="post">
            <input type="text" name="url" size="80">
            <input type="submit" value="Add">
          </form> 
        </td><td class="logout">
          <a href="/list">Settings</a>
          <a href="{{ logout }}">Logout</a>
      </td></tr>
    </table>
    {% for entry in entries %}
      <table width="800px" class="entry">
      <tr><td class="title"> [{{ entry.date|date:"d/M" }}] 
          <a href="{{ entry.link }}">{{ entry.title }}</a>
        </td><td rowspan="3" class="image">
         <div id="content{{ forloop.counter }}">Loading...</div>
        </td></tr>
      <tr><td class="kw">        
          <span class="source">{{ entry.source|slice:":20" }}</span>
         >> {{ entry.terms|slice:":4"|join:" / " }}
      </td></tr>
      <tr><td class="summary" width="700px" >
        {{ entry.content }}
      </td></tr>
    </table>
     {% endfor%}
  </body>
</html>
